package com.woniuxy.trade.service;

import com.woniuxy.common.dto.ProductDto;
import com.woniuxy.common.dto.RoomDto;
import com.woniuxy.common.exceptions.BusinessException;
import com.woniuxy.common.result.enums.impl.BusinessCode;
import com.woniuxy.common.utils.RedisUtils;
import com.woniuxy.common.utils.TokenUtil;
import com.woniuxy.common.vo.TokenVo;
import com.woniuxy.trade.entity.Product;
import com.woniuxy.trade.entity.Room;
import com.woniuxy.trade.mapper.ProductMapper;
import com.woniuxy.trade.mapper.RoomMapper;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SellerService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 卖家上传商品
     * @param productDto
     * @return
     */
    public void uploadPro(ProductDto productDto) {
        //从token中拿到当前用户信息
        TokenVo tokenVo = TokenUtil.getTokenVo();
        //封装商品对象
        Product product = new Product();
        product.setPname(productDto.getPname());
        product.setDetail(productDto.getDetail());
        product.setUserid(tokenVo.getId());
        product.setUseraccount(tokenVo.getUserName());
        product.setStatus(0);
        productMapper.insert(product);
    }
    /**
     * 卖家拍卖商品
     */
    public void salePro(RoomDto roomDto) {
        //封装拍卖房间对象
        Product product = productMapper.selectByPrimaryKey(roomDto.getProductid());
        if(product.getStatus() == 1 || product.getStatus() == 2){
            throw new BusinessException(BusinessCode.SALEPRO_FAIL);
        }
        Room room = new Room();
        room.setDepositrate(roomDto.getDepositrate());
        room.setStartprice(roomDto.getStartprice());
        room.setStepprice(roomDto.getStepprice());
        room.setCurrprice(roomDto.getCurrprice());
        room.setStartdate(roomDto.getStartdate());
        room.setEnddate(roomDto.getEnddate());
        room.setProductid(roomDto.getProductid());
        room.setProductname(roomDto.getProductname());
        room.setStatus(0);
        //添加拍卖房间
        roomMapper.insert(room);
        //修改商品状态为正在竞拍
        product.setStatus(1);
        productMapper.updateByPrimaryKey(product);
        //倒计时开始拍卖
        long current = System.currentTimeMillis();
        Date startdate = roomDto.getStartdate();
        long startTime = startdate.getTime();
        long time = startTime - current;
        redisUtils.set(room.getId().toString(),"拍卖前倒计时",time/1000);
        //发送带有过期时间的MQ消息，采用死信队列
        System.err.println("拍卖前倒计时:"+time);
        rabbitTemplate.convertAndSend("NORMAL_EXCHANGE","NORMAL_ROUTING_KEY",room,
                message -> {
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            message.getMessageProperties().setExpiration(time+"");
            return message;
        });
    }
}
